package javatest;

/**
 * 研究类对象的锁 和类实例锁
 * 类对象的锁大
 *
 */
public class ClassLockAndInstanceLock {

    private static final Object lock = new Object();

    public static void main(String[] args) {
        ClassLockAndInstanceLock classLock = new ClassLockAndInstanceLock();
        ClassLockAndInstanceLock classLock2 = new ClassLockAndInstanceLock();
        Thread A = new Thread() {
            @Override
            public void run() {
                System.out.println("Thread a start run");
                classLock.testA();
            }
        };
        Thread B = new Thread() {
            @Override
            public void run() {
                System.out.println("Thread b start run");
                classLock.testB();
            }
        };
        A.start();
        B.start();
        try {
            A.join();
            B.join();
            System.out.println("end of main thread");
        } catch (Exception e) {
        }
    }

    public static synchronized void testA() {
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + " get lock by A ");
        try {
            Thread.sleep(1000);
            System.out.println(threadName + " release lock by A ");

        } catch (InterruptedException e) {
        }
    }

    public synchronized void testB() {
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + " get lock by B ");
        try {
            Thread.sleep(300);
            System.out.println(threadName + " release lock by B ");
        } catch (InterruptedException e) {
        }
    }

    public String concat(String s1, String s2, String s3) {
        return s1 + s2 + s3;
    }

    public static void normalC() {
        synchronized (lock) {
            String threadName = Thread.currentThread().getName();
            System.out.println(threadName + " execute normal C ");
        }
    }
}
